<!-- eslint-disable vue/no-use-v-if-with-v-for -->
<template>
  <div class="container">
    <!-- 基础题库 -->
    <div class="bgc">
      <div class="top">
        <div class="illustrate">说明：目前支持学科和关键字条件筛选</div>
        <div class="top_right">
          <el-button
            type="success"
            @click="$router.push('/test-question-input')"
          >
            <i class="el-icon-edit" />
            新增试题
          </el-button>
        </div>
      </div>
      <!-- form表单  搜索 -->
      <el-form :model="formInline" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select
                v-model="formInline.subjectID"
                placeholder="请选择"
                @change="SubjectsFn"
              >
                <el-option
                  v-for="item in SubjectsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="formInline.catalogID" placeholder="请选择">
                <div v-if="formInline.subjectID">
                  <el-option
                    v-for="item in DirectoryList"
                    :key="item.id"
                    :label="item.directoryName"
                    :value="item.id"
                  />
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="formInline.tags" placeholder="请选择">
                <div v-if="formInline.subjectID">
                  <el-option
                    v-for="item in TagsList"
                    :key="item.id"
                    :label="item.tagName"
                    :value="item.id"
                  />
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                v-model="formInline.keyword"
                placeholder="根据题干搜索"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="formInline.questionType" placeholder="请选择">
                <el-option
                  v-for="item in typesOf"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="formInline.difficulty" placeholder="请选择">
                <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="formInline.direction" placeholder="请选择">
                <el-option
                  v-for="item in directionlist"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="formInline.creatorID" placeholder="请选择">
                <el-option
                  v-for="item in enterlist"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="题干备注">
              <el-input v-model="formInline.remarks" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="formInline.shortName" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-row :gutter="10">
                <el-col :span="22">
                  <el-col :span="12">
                    <el-select
                      v-model="formInline.province"
                      placeholder="请选择"
                      @change="citychange"
                    >
                      <el-option
                        v-for="(item, index) in city"
                        :key="index"
                        :label="item"
                        :value="item"
                      />
                    </el-select>
                  </el-col>
                  <el-col :span="12">
                    <el-select v-model="formInline.city" placeholder="请选择">
                      <div v-if="formInline.province">
                        <el-option
                          v-for="(item, index) in citys"
                          :key="index"
                          :label="item"
                          :value="item"
                        />
                      </div>
                    </el-select>
                  </el-col>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div style="float: right">
              <el-button @click="closeFn">清除</el-button>
              <el-button type="primary" @click="searchFn">搜索</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
      数据一共 {{ total }} 条
    </div>
    <div class="bgc">
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        :header-row-style="{ 'background-color': '#000' }"
      >
        <el-table-column prop="number" label="试题编号" width="240" />
        <el-table-column prop="subject" label="学科" width="120" />
        <el-table-column prop="catalog" label="目录" width="120" />
        <el-table-column prop="questionType" label="题型" width="60">
          <template v-slot="{ row }">
            {{
              row.questionType === '1'
                ? '单选'
                : row.questionType === '2'
                  ? '多选'
                  : '简答'
            }}
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="140">
          <template v-slot="{ row }">
            <span v-html="row.question" />
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="160">
          <template v-slot="{ row }">
            <span>{{ row.addDate | datatime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="60">
          <template v-slot="{ row }">
            {{
              row.questionType === '1'
                ? '简单'
                : row.questionType === '2'
                  ? '一般'
                  : '困难'
            }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人" width="100" />
        <el-table-column prop="address" label="操作" width="240">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              plain
              @click="previewFn(row.id)"
            />
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              plain
              @click="editFn(row.id)"
            />
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delRand(row.id)"
            />
            <el-button
              type="warning"
              icon="el-icon-check"
              circle
              plain
              @click="patchChoice(row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-sizes="[10, 5, 2, 1]"
          :page-size="formInline.pagesize"
          layout="->, prev,pager,next,sizes,jumper"
          :current-page="formInline.page"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <!-- 组件 -->
      <preview :show-dialog.sync="showDialog" :question-id="questionId" />
    </div>
  </div>
</template>
<script>
import {
  getBankListApi,
  delRandomsApi,
  patchChoiceApi
} from '@/api/questionBank'
import {
  getSubjectsListApi,
  getDirectoryListApi,
  getTagsListApi
} from '@/api/subject'
import { getUserListSimpleApi } from '@/api/backstage'
import preview from './preview.vue'
import { provinces } from '@/utils/city'
import { citys } from '@/utils/city'
export default {
  components: { preview },
  data() {
    return {
      // 搜索表单
      formInline: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: null, // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '' // 企业所在城市
      },
      total: 0, // 数据总条数
      tableData: [],
      SubjectsId: null, // 学科id
      SubjectsList: [], // 学科列表
      DirectoryList: [], // 目录列表
      TagsList: [], // 标签列表
      showDialog: false, // 弹层状态
      questionId: null, // 试题id
      // 城市
      city: [],
      citys: [],
      // 试题类型
      typesOf: [
        { value: 1, label: '单选' },
        { value: 2, label: '多选' },
        { value: 3, label: '简答' }
      ],
      // 难度
      difficulty: [
        { value: 1, label: '简单' },
        { value: 2, label: '一般' },
        { value: 3, label: '困难' }
      ],
      // 方向
      directionlist: [
        { name: 'o2o' },
        { name: '外包服务' },
        { name: '企业服务' },
        { name: '互联网金融' },
        { name: '企业咨询' },
        { name: '互联网' },
        { name: '电子商务' },
        { name: '其他' }
      ],
      // 录入人
      enterlist: []
    }
  },
  async created() {
    const { data } = await getSubjectsListApi()
    this.SubjectsList = data
    const user = await getUserListSimpleApi()
    this.enterlist = user.data
    this.getBankList()
    this.city = provinces()
  },
  methods: {
    // 基础题库列表方法
    async getBankList() {
      const { data } = await getBankListApi(this.formInline)
      this.tableData = data.items
      this.total = data.counts
    },
    // 页码发生变化
    handleSizeChange(val) {
      this.formInline.pagesize = val
      this.getBankList()
    },
    handleCurrentChange(val) {
      this.formInline.page = val
      this.getBankList()
    },
    // 清空搜索框
    closeFn() {
      this.formInline = {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: null, // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '' // 企业所在城市
      }
      this.getBankList()
    },
    // 城市
    async citychange(val) {
      this.citys = citys(val)
    },
    // 搜索
    searchFn() {
      this.getBankList()
    },
    // 学科选中
    async SubjectsFn(id) {
      this.SubjectsId = id
      const { data } = await getDirectoryListApi(id)
      this.DirectoryList = data.items
      const tags = await getTagsListApi(id)
      this.TagsList = tags.data.items
    },
    // 预览
    previewFn(id) {
      this.questionId = id
      this.showDialog = true
    },
    // 修改
    editFn(id) {
      this.$router.push({
        path: '/test-question-input',
        query: {
          id: id
        }
      })
    },
    // 删除弹出框
    delRand(id) {
      this.$confirm('此操作将删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delRandomsApi(id)
          this.getBankList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 加入精选
    patchChoice(id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await patchChoiceApi(id, 1)
          this.getBankList()
          this.$message({
            type: 'success',
            message: '加入精选成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消加入精选'
          })
        })
    }
  }
}
</script>
<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.bgc {
  background-color: #fff;
  padding: 10px;
  border-radius: 10px;
}
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  .illustrate {
    color: rgb(221, 104, 104);
    font-size: 12px;
  }
}
.el-select {
  width: 100%;
}
</style>
